package Day08;

import javax.swing.*;
import javax.xml.transform.Source;
import java.util.ArrayList;
import java.util.Scanner;

public class GonvernStudent {
    public static void main(String[] args) {
        ArrayList<SStudent> list = new ArrayList<>();
        loop:while(true){
            System.out.println("------欢迎来到学生管理系统------");
            System.out.println("1: 添加学生");
            System.out.println("2: 删除学生");
            System.out.println("3: 修改学生");
            System.out.println("4：查询学生");
            System.out.println("5： 退出");
            System.out.println("请输入你的选择");
            Scanner sc = new Scanner(System.in);
            String stu = sc.next();
            switch(stu){
                case "1":
                    addStudent(list);
                    continue;
                case "2":
                    deleteStudent(list);
                    continue;
                case "3":
                    updateStudent(list);
                    continue;
                case "4":
                    selectStudent(list);
                    continue;
                case "5":
                    System.out.println("退出");
                    break loop;
                default:
                    System.out.println("没有这个选项");
                    continue;
            }

        }
    }


    public static void addStudent(ArrayList<SStudent> list){

        SStudent s = new SStudent();
        Scanner sc = new Scanner(System.in);

        String id = null;

        while(true){
            System.out.println("请输入学生的ID");
            id = sc.next();
            boolean flag = contains(list,id);
            if(flag){
                System.out.println("ID已经重复，请重新输入");
            }else{
                s.setId(id);
                break;
            }
        }


        System.out.println("请输入学生的姓名");
        String name = sc.next();
        s.setName(name);

        System.out.println("请输入学生的年龄");
        int age = sc.nextInt();
        s.setAge(age);

        System.out.println("请输入学生的家庭住址");
        String address = sc.next();
        s.setAddress(address);

        list.add(s);
        System.out.println("信息输入成功");

    }


    //删除操作
    public static void deleteStudent(ArrayList<SStudent> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的ID");
        String id = sc.next();
        //查询ID在集合中的索引
        int index = getIndex(list,id);
        //对index进行判断
        //如果-1，就表示不存在，结束方法，回到初始菜单
        if(index >= 0){
            //大于0，表示存在，直接删除
            list.remove(index);
            System.out.println("ID为:" + id + "的学生删除成功");
        }else{
            System.out.println("ID不存在，删除失败");
        }




    }

    public static void updateStudent(ArrayList<SStudent> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的学生的ID");
        String id = sc.next();
        int index = getIndex(list,id);



        if(index == -1){
            System.out.println("要修改的ID为" + id + "不存在，请重新 输入");
            return;
        }
        //以下代码表示当前ID是存在的,获取要修改的学生对象
        SStudent stu = list.get(index);

        //输入其他的信息并修改
        System.out.println("请输入要修改的学生姓名");
        String newName = sc.next();
        stu.setName(newName);

        System.out.println("请输入要修改的学生年龄");
        int age = sc.nextInt();
        stu.setAge(age);

        System.out.println("请输入需要修改的学生的家庭住址");
        String newAddress = sc.next();
        stu.setAddress(newAddress);

        list.add(stu);
    }

    public static void selectStudent(ArrayList<SStudent> list){
        if(list.size() == 0){
            System.out.println("当前没有学生，请添加之后再查询");
        }

        System.out.println("ID      姓名  年龄  家庭住址" );
        for (int i = 0; i < list.size(); i++) {
            SStudent stu = list.get(i);
            System.out.println(stu.getId() +"   " + stu.getName()
                    + "   " + stu.getAge() + "    " + stu.getAddress());
        }
    }


    //查看ID是否重复，如果重复，返回true，没有重复则返回false；
    public static boolean contains(ArrayList<SStudent> list, String id){
        int n = getIndex(list,id);
        if(n >= 0){
            return true;
        }else{
            return false;
        }
    }

    //通过ID获取索引的方法
    public static int getIndex(ArrayList<SStudent> list, String id){
        for (int i = 0; i < list.size(); i++) {
            SStudent stu = list.get(i);
            String sid = stu.getId();
            if(sid.equals(id)){
                return i;
            }
        }
        return -1;
    }
}